﻿using System.Linq;

namespace Model
{
    public static class ContextExtension
    {
        public static void ClearDatabase(this ActualEntities context)
        {
            foreach (var item in context.Assemblies)
            {
                context.DeleteObject(item);
            }
            context.SaveChanges();
            foreach (var item in context.CallGraphEdge_AssemblyDependency)
            {
                context.DeleteObject(item);
            }
            context.SaveChanges();
            foreach (var item in context.CallGraphEdge_MemberDependency)
            {
                context.DeleteObject(item);
            }
            context.SaveChanges();
            foreach (var item in context.CallGraphEdge_ModuleDependency)
            {
                context.DeleteObject(item);
            }
            context.SaveChanges();
            foreach (var item in context.CallGraphEdge_NamespaceDependency)
            {
                context.DeleteObject(item);
            }
            context.SaveChanges();
            foreach (var item in context.CallGraphEdges)
            {
                context.DeleteObject(item);
            }
            context.SaveChanges();
            foreach (var item in context.Categories)
            {
                context.DeleteObject(item);
            }
            context.SaveChanges();
            foreach (var item in context.Category_Subcharacteristic)
            {
                context.DeleteObject(item);
            }
            context.SaveChanges();
            foreach (var item in context.FxCopIssues)
            {
                context.DeleteObject(item);
            }
            context.SaveChanges();
            foreach (var item in context.FxCopResolutions)
            {
                context.DeleteObject(item);
            }
            context.SaveChanges();
            foreach (var item in context.Mappings)
            {
                context.DeleteObject(item);
            }
            context.SaveChanges();
            foreach (var item in context.Members)
            {
                context.DeleteObject(item);
            }
            context.SaveChanges();
            foreach (var item in context.Message_FxCopMessage)
            {
                context.DeleteObject(item);
            }
            context.SaveChanges();
            foreach (var item in context.Messages)
            {
                context.DeleteObject(item);
            }
            context.SaveChanges();
            foreach (var item in context.Modules)
            {
                context.DeleteObject(item);
            }
            context.SaveChanges();
            foreach (var item in context.Namespaces)
            {
                context.DeleteObject(item);
            }
            context.SaveChanges();
            foreach (var item in context.Projects)
            {
                context.DeleteObject(item);
            }
            context.SaveChanges();
            foreach (var item in context.QualityCharacteristics)
            {
                context.DeleteObject(item);
            }
            context.SaveChanges();
            foreach (var item in context.Rule_FxCopRule)
            {
                context.DeleteObject(item);
            }
            context.SaveChanges();
            foreach (var item in context.Rules)
            {
                context.DeleteObject(item);
            }
            context.SaveChanges();
            foreach (var item in context.Types)
            {
                context.DeleteObject(item);
            }
            context.SaveChanges();
        }

        public static void CalculateQualityProfile(this ActualEntities context)
        {
            foreach (var item in context.QualityProfileItems)
            {
                int w = item.Category.Effects.Where(e => e.Answer.IsChecked == true).Sum(e => e.Weight);
                item.Weight = w;
            }
            context.SaveChanges();
        }
    }
}